package io.ede.common.utils;

import io.ede.common.exception.RRException;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * fii文件操作类
 */
public class FiiUtils {

    /**
     * 获取动作组
     * @param fii
     * @return
     */
    public static List<HashMap<String, Object>> getFiiActions(String fii){
        SAXReader reader = new SAXReader();
        Document document;
        List<HashMap<String, Object>> actionList = new ArrayList<>();
        try {
            document = reader.read(new ByteArrayInputStream(fii.getBytes("utf-8")));
            //获取root(根)节点
            Element root = document.getRootElement();
            List<Element> actions = XmlParser.getNameElement(root, "Actions");
            for (Element element : actions) {
                String actionname = XmlParser.attrValue(element, "actionname");
                HashMap<String, Object> actionMap = new HashMap<>();
                actionMap.put("actionname", actionname);
                //无人机
                List<Element> flightList = XmlParser.getNameElement(root, "ActionFlight");
                for (Element ele : flightList) {
                    String actionfname = XmlParser.attrValue(ele, "actionfname");
                    if (actionfname.contains(actionname)) {
                        actionfname = actionfname.replace(actionname, "");
                        actionMap.put("actionfname", actionfname);
                    }
                }
                //编号
                List<Element> actionfidList = XmlParser.getNameElement(root, "ActionFlightID");
                for (Element ele : actionfidList) {
                    String actionfid = XmlParser.attrValue(ele, "actionfid");
                    if (actionfid.contains(actionname)) {
                        actionMap.put("actionfid", actionfid);
                    }
                }
                //x
                List<Element> actionfXList = XmlParser.getNameElement(root, "ActionFlightPosX");
                for (Element ele : actionfXList) {
                    String actionfX = XmlParser.attrValue(ele, "actionfX");
                    if (actionfX.contains(actionname)) {
                        actionMap.put("actionfX", actionfX);
                    }
                }
                //y
                List<Element> actionfYList = XmlParser.getNameElement(root, "ActionFlightPosY");
                for (Element ele : actionfYList) {
                    String actionfY = XmlParser.attrValue(ele, "actionfY");
                    if (actionfY.contains(actionname)) {
                        actionMap.put("actionfY", actionfY);
                    }
                }
                //z
                List<Element> actionfZList = XmlParser.getNameElement(root, "ActionFlightPosZ");
                for (Element ele : actionfZList) {
                    String actionfZ = XmlParser.attrValue(ele, "actionfZ");
                    if (actionfZ.contains(actionname)) {
                        actionMap.put("actionfZ", actionfZ);
                    }
                }
                actionList.add(actionMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return actionList;
    }

    /**
     * 生成动作组名称
     * @param fii
     * @return
     */
    public static String createNewAction(String fii){
        String newName = "动作组1";
        List<HashMap<String, Object>> actionList = getFiiActions(fii);
        if(actionList!=null&&actionList.size()>0){
            HashMap<String, Object> actionMap = actionList.get(actionList.size()-1);
            String name = (String)actionMap.get("actionname");
            String id = name.replace("动作组","");
            newName = "动作组"+(Integer.parseInt(id)+1);
        }
        return newName;
    }

    /**
     * 修改工程fii文件内容
     * @param fii
     * @param newName
     */
    public static String addGroup(String fii,String newName){
        String xml = "";
        try {
            SAXReader reader = new SAXReader();
            Document document;
            document = reader.read(new ByteArrayInputStream(fii.getBytes("utf-8")));
            //获取root(根)节点
            Element root = document.getRootElement();
            XmlParser.addElement(root,"Actions","actionname",newName,"");
            xml = document.asXML();
            System.out.println(xml);
        }catch (Exception e){
            throw new RRException("fii解析出错");
        }
        return xml;
    }

}
